package com.poi.qianmi.fetch;

import com.monitorjbl.xlsx.StreamingReader;
import com.poi.qianmi.handle.ExcelEventParser;
import com.poi.qianmi.jol.JolDetection;
import com.poi.qianmi.model.User;
import com.poi.qianmi.path.GlobalPath;
import com.poi.qianmi.response.Response;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname Fetch3
 * @Description TODO
 * @Date 2019/9/20 1:52 下午
 * @Created by xiaocai
 */
@RestController
public class Fetch3 {
    @GetMapping("/fetch3/{num}")
    public Map fetch3(@PathVariable("num") int num) {
        long startTime = System.currentTimeMillis();
        List<User> userList;
        InputStream is;
        try {
            is = new FileInputStream(new File(GlobalPath.getInstance(num)));
            Workbook workbook = StreamingReader.builder()
                    .rowCacheSize(100)    // number of rows to keep in memory (defaults to 10)
                    .bufferSize(4096)     // buffer size to use when reading InputStream to file (defaults to 1024)
                    .open(is);            // InputStream or File for XLSX file (required)

            userList = new ArrayList();
            for (Sheet sheet : workbook) {
                for (Row r : sheet) {
                    User user = User.builder().build();
                    for (Cell c : r) {
                        if (c.getColumnIndex() == 0) {
                            user.setName(c.getStringCellValue());
                        } else if (c.getColumnIndex() == 1) {
                            user.setAdress(c.getStringCellValue());
                        } else if (c.getColumnIndex() == 2) {
                            user.setVip(c.getStringCellValue());
                        } else if (c.getColumnIndex() == 3) {
                            user.setRemark(c.getStringCellValue());
                        }
                    }
                    userList.add(user);
                }
            }
            long endTime = System.currentTimeMillis();
            System.out.println("***************程序已经结束等待内存测试***************");
//            Thread.sleep(20000);
            return Response.ok(userList.size(), endTime - startTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.fail();
    }

    @GetMapping("/fetch4/{num}")
    public Map fetch4(@PathVariable("num") int num) {
        long startTime = System.currentTimeMillis();
        try {
            List<User> userList = new ArrayList<>();
            new ExcelEventParser(GlobalPath.getInstance(num)).setHandler(new ExcelEventParser.SimpleSheetContentsHandler() {
                @Override
                public void endRow(int rowNum) {
                    User user = User.builder().name(row.get(0))
                            .adress(row.get(1))
                            .vip(row.get(2))
                            .remark(row.get(3))
                            .build();
                    userList.add(user);
                }
            }).parse();

            long endTime = System.currentTimeMillis();
            System.out.println("***************程序已经结束等待内存测试***************");
//            Thread.sleep(20000);
            return Response.ok(1, endTime - startTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.fail();
    }
}
